package com.example.mybatisplus.task;

import com.example.mybatisplus.mapper.BedManagmentMapper;
import com.example.mybatisplus.mapper.BuildingManagementMapper;
import com.example.mybatisplus.model.domain.BedManagment;
import com.example.mybatisplus.model.domain.BuildingManagement;
import com.example.mybatisplus.service.BedManagmentService;
import com.example.mybatisplus.service.BuildingManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@EnableScheduling
public class CountStatTask {

    @Autowired
    private BedManagmentMapper bedManagmentMapper;
    @Autowired
    private BuildingManagementMapper buildingManagementMapper;

    @Scheduled(fixedDelay = 300000)
    public void update() {
// todo: 1.更新房间数量
// todo: 2.更新楼栋床位数量
        List<BuildingManagement> buildingManagements = buildingManagementMapper.selectList(null);
        for (BuildingManagement buildingManagement : buildingManagements) {
            Integer roomCount= buildingManagementMapper.countRoom(buildingManagement.getBuildingId());
            Integer bedCount= buildingManagementMapper.countBed(buildingManagement.getBuildingId());
            buildingManagement.setTotalRoom(roomCount);
            buildingManagement.setTotalBed(bedCount);
            buildingManagementMapper.updateById(buildingManagement);
        }
    }
}
